package com.twu.biblioteca;

/**
 * Created by echester on 16/07/2014.
 */
public class Book {
    private String title, author;
    private int yearPublished;
    private boolean isAvailable;

    public Book(String title, String author, int yearPublished) {
        this.title = title;
        this.author = author;

        if(yearPublished <= 2014)
            this.yearPublished = yearPublished;
        else
            throw new IllegalStateException("The year published has not already occurred.");
        isAvailable = true;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public int getYearPublished() {
        return this.yearPublished;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void rent() {
        if(!isAvailable) throw new IllegalStateException("This book has already been rented, and is unavailable");
        else isAvailable = false;
    }

    public void returnBook() {
        isAvailable = true;
    }

    @Override
    public String toString() {
        return title + ", " + author + " [" + yearPublished + "]";
    }

    @Override
    public boolean equals(Object obj) {
        Book otherBook = (Book) obj;
        return (otherBook.title.equals(this.title) &&
                otherBook.author.equals(this.author) &&
                otherBook.yearPublished==this.yearPublished);
    }

}
